/* ************************************************************************ */
/*                                                                          */
/*  Copyright (c)2005 Russell Weir | damonsbane _at_ gmail                  */
/*                                                                          */
/* This library is free software; you can redistribute it and/or            */
/* modify it under the terms of the GNU Lesser General Public               */
/* License as published by the Free Software Foundation; either             */
/* version 3.0 of the License, or (at your option) any later version.       */
/*                                                                          */
/* This library is distributed in the hope that it will be useful,          */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        */
/* Lesser General Public License or the LICENSE file for more details.      */
/*                                                                          */
/* ************************************************************************ */

package tzparser;
import altdate.TimeSpecification;

/*
     FORMAT
           The format for time zone abbreviations in this time
           zone.  The pair of characters %s is used to show where
           the "variable part" of the time zone abbreviation
           goes.  Alternately, a slash (/) separates standard and
           daylight abbreviations.
*/
enum ZoneFormatType {
	STATIC;
	ALTERNATES;
	PARSED;
}

class ZoneFormat {
	var type : ZoneFormatType;
	var stdString : String;
	var dstString : String;
	var other : String;

	public function new() {
		type = STATIC;
		stdString = "";
		dstString = "";
		other = "";
	}

	public function getValue(offset:TimeSpecification, ?ruleString : String) : String {
		switch(type) {
		case STATIC:
			return other;
		case PARSED:
			return StringTools.replace(other,"%s",ruleString);
		case ALTERNATES:
			if(offset.getValue() == 0)
				return stdString;
		}
		return dstString;
	}

	public function getType() : ZoneFormatType { return type; }

	public static function parse(atom:String) : ZoneFormat {
		var zf = new ZoneFormat();
		var r = ~/^([A-z]+)\/([A-z]+)$/;
		if(r.match(atom)) {
			zf.type = ALTERNATES;
			zf.stdString = r.matched(1);
			zf.dstString = r.matched(2);
			return zf;
		}

		var p = atom.indexOf("%s");
		if( p >= 0 ) {
			zf.type = PARSED;
			zf.other = atom;
			return zf;
		}
		zf.other = atom;
		return zf;
	}
}